package com.example.bleuesprit.parkAndroidClient;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.bleuesprit.parkAndroidClient.net.ExampleClient;

import org.java_websocket.drafts.Draft_17;

import java.net.URI;
import java.net.URISyntaxException;


/**
 * Created by bleuesprit on 2015/10/28.
 */



public class TestActivity extends Activity {



    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {
                Bundle data=msg.getData();
                String message=data.getString("message");
                Toast.makeText(TestActivity.this,message,Toast.LENGTH_LONG).show();
            }
        }

    };


    Button sendBnt;
    private ExampleClient c=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        sendBnt=(Button)findViewById(R.id.send);


        ExampleClient c = null;
        try {
            c = new ExampleClient( new URI( "ws://219.219.221.14:8080/ParkTest/Websocket1Action" ), new Draft_17(),handler );

        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        final ExampleClient finalC = c;
        sendBnt.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            if (finalC != null) {

                                finalC.connectBlocking();
                                finalC.send("handshake");
                            }
                            }catch(InterruptedException e){
                                e.printStackTrace();
                            }


                    }


                }
        );
    }




}
